using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using CodRevit.ViewModels;
using CodRevit.Views;

namespace CodRevit.Commands
{
    [Transaction(TransactionMode.Manual)]
    public class SimpleRoadCommand : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            // 创建ExternalEvent和Handler
            var roadHandler = new SimpleRoadHandler(commandData.Application.ActiveUIDocument.Document);
            var externalEvent = ExternalEvent.Create(roadHandler);

            // 创建窗口和ViewModel
            var window = new SimpleRoadView();
            var viewModel = new SimpleRoadViewModel(
                commandData.Application,
                window,
                externalEvent,
                roadHandler);

            window.DataContext = viewModel;

            var positionedWindow = App.LeftTopWindow(commandData, window);
            if (positionedWindow == null || positionedWindow is not Views.SimpleRoadView)
            {
                return Result.Failed;
            }
            window = (SimpleRoadView)positionedWindow;
            window.Show();
            return Result.Succeeded;
        }
    }
}